﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Basic_sprite_code
{
    class Sprite : DrawableGameComponent
    {
        public Texture2D image;
        public Vector2 center;

        /// <summary>
        /// Position of the designated center of the sprite on screen; (0,0)= upper-left-hand of screen.
        /// </summary>
        public Vector2 position;
        /// <summary>
        /// Translational speed of the sprite on screen, in pixels per second.
        /// </summary>
        public Vector2 velocity;
        /// <summary>
        /// Rotation of the sprite on screen, in degrees counter-clockwise.
        /// </summary>
        public float orientation = 0f;
        /// <summary>
        /// Scale factor of the sprite on screen (1=normal, 2=double-size, 0.5f=half-size).
        /// </summary>
        public float scale = 1f;
        /// <summary>
        /// Rate of rotation of the object on screen, in degrees per second counter-clockwise.
        /// </summary>
        public float angularVelocity = 0f;

        public float zDepth = 1.0f; // [KEN] use this to manage z draw order, using 1.0 as default for now


        protected DarkLordsApprentice game;

        /// <summary>
        /// Creates a sprite given the name of its image in the Content directory
        /// </summary>
        /// <param name="g">The Game object for the current game (fill in with "this")</param>
        /// <param name="imageName">Name of the image in the Content directory; don't include extension, like .jpg.</param>
        /// <param name="position">Starting position on screen</param>
        public Sprite(DarkLordsApprentice g, string imageName, Vector2 position, Vector2 center)
            : this(g, g.Content.Load<Texture2D>(imageName), position, center)
        { }

        /// <summary>
        /// Creates a sprite given its texture.
        /// </summary>
        /// <param name="g">The Game object for the current game</param>
        /// <param name="image">The texture to display in this sprite</param>
        /// <param name="position">Starting location for the sprite.</param>
        public Sprite(DarkLordsApprentice g, Texture2D image, Vector2 position, Vector2 center) : base(g)
        {
            this.image = image;
            this.position = position;
            this.center = center;
            velocity = Vector2.Zero;
            angularVelocity = 0f;
            game = g;
        }

        /// <summary>
        /// Draws the sprite on screen.
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            game.spriteBatch.Draw(
                image,
                position,
                null,
                Color.White,
                (float)(Math.PI*2*orientation/360f),
                center,
                scale,
                SpriteEffects.None,
                zDepth);                // [KEN] used zDepth here as reachable variable for any method that wants to manage depth drawing ..
            base.Draw(gameTime);
        }

        /// <summary>
        /// Updates position and orientation based on velocity and angular-velocity.
        /// </summary>
        /// <param name="gameTime">The time passed in by XNA to the original update routine.</param>
        public override void Update(GameTime gameTime)
        {
            float frameTime = (float)(gameTime.ElapsedGameTime.TotalSeconds);
            position += velocity * frameTime;
            orientation += angularVelocity * frameTime;
            base.Update(gameTime);
        }

        public IEnumerable<Sprite> AllSprites {
            get {
                foreach (GameComponent g in game.Components) {
                    if (g is Sprite)
                        yield return (Sprite)g;
                }
            }
        }
    }
}
